/**
 *
 */
package com.sihai.ehr.web.action.ajax;

import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.Grade;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.DutyService;
import com.sihai.ehr.service.GradeService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.util.List;

/**
 * @author wxn
 * @since 2012-6-7 21:56
 */
public class RankAction extends EHRAction {

    private static final long serialVersionUID = 2121333028480225454L;

    @BeanInject
    private DutyService dutyService;
    @BeanInject
    private GradeService gradeService;
    
    private Long organizationId;
    private List<Duty> duties;

    private List<Grade> grades;

    //得到某一组织结构下的职务列表
    @Action(value = "/ajax/duty-list", results = {
            @Result(name = "success", type = "json", params = {"root", "duties"})
    })
    public String findDuties() {
        duties = dutyService.search(organizationId ,null, null);
        return SUCCESS;
    }

    //得到某一职务下的职等
    @Action(value = "/ajax/grade-list", results = {@Result(name = "success", type = "json", params = {"root", "grades"})
    })
    public String findGrade() {
        Long dutyId = NumberUtils.createLong(getParameter("dutyId"));
        if(dutyId == null || dutyId <= 0){
            grades = null;
        }

        grades = gradeService.findGradeByDutyId(dutyId ,null, null);
        return SUCCESS;
    }

    //得到某一职务下的职级
    @Action(value = "/ajax/grade-level-list", results = {@Result(name = "success", type = "json", params = {"root", "grades"})
    })
    public String findGradeLevel() {
        Long gradeId = NumberUtils.createLong(getParameter("gradeId"));
        if(gradeId == null || gradeId <= 0){
            grades = null;
            return SUCCESS;
        }
        Grade g = gradeService.get(gradeId);
        if(g == null){
            grades = null;
            return SUCCESS;
        }
        grades = gradeService.findGradeLevelByGrade(g ,null, null);
        return SUCCESS;
    }

    //得到某一职务下的职等职级
    @Action(value = "/ajax/grade-and-level", results = {@Result(name = "success", type = "json", params = {"root", "grades"})
    })
    public String findGradeAndLevel() {
        Long dutyId = NumberUtils.createLong(getParameter("dutyId"));
        if(dutyId == null || dutyId <= 0){
            return SUCCESS;
        }
        grades = gradeService.findGradeAndLevelByDuty(dutyId ,null, null);
        return SUCCESS;
    }

    public Long getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(Long organizationId) {
        this.organizationId = organizationId;
    }

    public List<Duty> getDuties() {
        return duties;
    }

    public void setDuties(List<Duty> duties) {
        this.duties = duties;
    }

    public List<Grade> getGrades() {
        return grades;
    }
}
